package com.gscitysfy.cus.drain.modules.warning.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cus.drain.modules.warning.entity.DrainSignal;
import com.gscitysfy.cus.drain.modules.warning.mapper.DrainSignalMapper;
import com.gscitysfy.cus.drain.modules.warning.service.IDrainSignalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author user
 * @since 2022-10-22 15:33:42
 */
@Service
@EnableScheduling()
public class DrainSignalServiceImpl extends ServiceImpl<DrainSignalMapper, DrainSignal> implements IDrainSignalService {
    @Resource
    private DrainSignalMapper drainSignalMapper;

    private  final String GETNEWESTDATA="http://59.203.18.20:10088/signal/getNewestData";

    @Override
    public Result<Object> getByPage(Page<DrainSignal> page, DrainSignal drainSignal) {
        IPage<DrainSignal> drainWaringSystemList =drainSignalMapper.getPageList(page,drainSignal);
        return Result.content(drainWaringSystemList);
    }

    @Scheduled(cron = "0 0 0/2 * * ? ")//每2小时触发一次
//    @Scheduled(cron = "0 0/1 * * * ?")
    public void getNewestData() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = HttpUtil.get(GETNEWESTDATA);
        if(s!=null){
            JSONObject jsonObject = JSONObject.parseObject(s);
            if("0".equals(jsonObject.get("returnCode").toString())){
                JSONArray ds1 = JSONObject.parseArray(jsonObject.get("ds").toString());
                for (Object o : ds1) {
                    JSONObject newsData = JSONObject.parseObject(o.toString());
                    DrainSignal drainSignal = new DrainSignal();
                    if("淮北".equals(newsData.get("city").toString())){
                        drainSignal.setWarningSignalLevel(newsData.get("level").toString());
                        drainSignal.setWarningSignalType(newsData.get("type").toString());
                        drainSignal.setWarningSignalContent(newsData.get("content").toString());
                        drainSignal.setWarningSignalTime(sdf2.parse(sdf2.format(sdf.parse(newsData.get("latestUpdateTime").toString()))));
                        drainSignal.setWarningSignalAlertId(newsData.get("alertId").toString());
                        drainSignal.setWarningSignalCity(newsData.get("city").toString());
                        drainSignal.setWarningSignalCounty(newsData.get("county").toString());
                        QueryWrapper<DrainSignal> queryWrapper = new QueryWrapper();
                        queryWrapper.eq("warning_signal_time",sdf2.parse(sdf2.format(sdf.parse(newsData.get("latestUpdateTime").toString()))));
                        queryWrapper.eq("warning_signal_type",newsData.get("type").toString());
                        queryWrapper.eq("warning_signal_level",newsData.get("level").toString());
                        queryWrapper.eq("warning_signal_county",newsData.get("county").toString());
                        List<DrainSignal> drainSignals = drainSignalMapper.selectList(queryWrapper);
                        if(drainSignals.size()>0){
                            drainSignal.setCmKid(drainSignals.get(0).getCmKid());
                        }
                        this.addOrUpdate(drainSignal);
                    }
                }
            }
        }
    }
    @Override
    public Result<Boolean> addOrUpdate(DrainSignal drainSignal) {
        return Result.content(this.saveOrUpdate(drainSignal));
    }

    @Override
    public List<String> getWarningSignalTypeList() {
        warningSignalType[] arr1 = warningSignalType.values();
        //利用for each循环进行打印
        List<String> objects = new ArrayList<>();
        for(warningSignalType item : arr1)
        {
            objects.add(item.toString());
        }
        return objects;
    }

    @Override
    public List<String> getWarningSignalLevel() {
        level[] arr1 = level.values();
        //利用for each循环进行打印
        List<String> objects = new ArrayList<>();
        for(level item : arr1)
        {
            objects.add(item.toString());
        }
        return objects;
    }

    public enum warningSignalType{
        台风,暴雨,暴雪,寒潮,大风,沙尘暴,高温,干旱,雷电,冰雹,霜冻,大雾,霾,道路结冰
    }

    public enum level{
        蓝,黄,橙,红
    }
}
